<template>
  <div>
    <a-table
      ref="table"
      size="middle"
      bordered
      rowKey="id"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="false"
      :row-selection="rowSelection"
      :customRow="handleClickRow"
      :row-class-name="tableRowClassName"
      style="height: 300px; overflow-x: hidden; overflow-y: auto"
    >
    </a-table>
    <ProjectModal ref="modalForm" @onSelect="onSelect"></ProjectModal>
  </div>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import ProjectModal from './ProjectModal.vue'
import columns from './indexColumns'

export default {
  name: 'AssistModal',
  mixins: [JeecgListMixin],
  components: {
    ProjectModal,
  },
  data() {
    return {
      queryParam: {},
      // 表头
      columns: columns(this),
      url: {
        list: '/sjyzzx/sjZxzyAssistAccount/queryAssistList',
      },
      reportIds: [],
      checkedRows: [],
      // 规定默认选中的行的key值(table把id绑定为key值 ，看每条数据id即可)
      checkedKeys: ['1'],
      firstRecord: {},
    }
  },
  computed: {
    rowSelection() {
      return {
        type: 'radio',
        getCheckboxProps: (record) => {
          if (this.checkedKeys.includes(record.id)) {
            record.index = 0
            this.checkedRows = [{ ...record }]
            console.log(this.checkedRows)
            this.firstRecord = Object.assign({}, record)
            //  默认选中
            this.$emit('input', record.assistCode)
            this.$emit('sendAssisCode', record)
          }
          return {
            props: {
              // disabled: record.name === 'Disabled User',
              // name: record.name,
              defaultChecked: this.checkedKeys.includes(record.id), // 该属性用于默认勾选处理
            },
          }
        },
        selectedRowKeys: this.checkedKeys,
        onChange: (selectedRowKeys, selectedRows) => {
          this.ProCessAssistName()
          this.reportIds = []
          this.checkedKeys = selectedRowKeys
          console.log(selectedRows)
          this.checkedRows = selectedRows
          console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows)
          this.$emit('input', selectedRows[0].assistCode)
          this.$emit('sendAssisCode', selectedRows[0])
        },
      }
    },
  },
  methods: {
    openSearchModel(record) {
      this.$refs.modalForm.edit(record)
      this.$refs.modalForm.isCheck = false
      this.$refs.modalForm.disableSubmit = false
    },
    handleClickRow(record, index) {
      return {
        on: {
          click: () => {
            console.log(111, record)
            if (this.checkedKeys != [] && this.checkedKeys[0] == record.id) {
              this.openSearchModel(record)
            }
          },
        },
      }
    },
    ProCessAssistName(val={assCode:'',assName:''}) {
      let index = this.dataSource.findIndex((item) => item.id == this.checkedRows[0].id)
      console.log(index);
      this.dataSource[index].assistItemCode = val.assCode
      this.dataSource[index].assistItemName = val.assName
      this.$emit('onSelectAssist', '')
    },
    onSelect(val) {
      let index = this.dataSource.findIndex((item) => item.id == this.checkedRows[0].id)
      let assistCode = val.assCode
      this.dataSource[index].assistItemCode = val.assCode
      this.dataSource[index].assistItemName = val.assName
      this.$emit('onSelectAssist', assistCode)
    },
    updateList() {
      this.loadData(1)
    },
    tableRowClassName(record, index) {
      // record.index = index
      // console.log(index)
    },
    clear() {
      this.ProCessAssistName()
      this.checkedRows = [{...this.firstRecord}]
      this.checkedKeys = this.$options.data().checkedKeys
      //  默认选中
      this.$emit('input', this.firstRecord.assistCode)
      this.$emit('sendAssisCode', this.firstRecord)
    },
  },
}
</script>
<style scoped>
</style>